export function pathToBase64(path) {
    return new Promise(function(resolve, reject) {
        if (typeof window === 'object' && 'document' in window) {
            if (typeof FileReader === 'function') {
                var xhr = new XMLHttpRequest()
                xhr.open('GET', path, true)
                xhr.responseType = 'blob'
                xhr.onload = function() {
                    if (this.status === 200) {
                        let fileReader = new FileReader()
                        fileReader.onload = function(e) {
                            resolve(e.target.result)
                        }
                        fileReader.onerror = reject
                        fileReader.readAsDataURL(this.response)
                    }
                }
                xhr.onerror = reject
                xhr.send()
                return
            }
            var canvas = document.createElement('canvas')
            var c2x = canvas.getContext('2d')
            var img = new Image
            img.onload = function() {
                canvas.width = img.width
                canvas.height = img.height
                c2x.drawImage(img, 0, 0)
                resolve(canvas.toDataURL())
                canvas.height = canvas.width = 0
            }
            img.onerror = reject
            img.src = path
            return
        }
        if (typeof plus === 'object') { //小白看向这里，此处为手机端图片处理真实区域。
            //官方压缩
			plus.zip.compressImage({
			//plus.zip.compressImg 官方Api地址 (http://www.html5plus.org/doc/zh_cn/zip.html)
				src:path,     		//你要压缩的图片路径
				dst:'_doc/a'+path, //你要生成的图片路径，路径不能与src相同，不然app端会报错
				quality:10         //图片质量[10-100]
			},function(e) {
				// console.log("压缩成功后再转base64",e)
				//压缩成功后再转base64
				plus.io.resolveLocalFileSystemURL(e.target, function(entry) {
					entry.file(function(file) {
						var fileReader = new plus.io.FileReader()
						fileReader.onload = function(data) {
							resolve(data.target.result)
						}
						fileReader.onerror = function(error) {
							reject(error)
						}
						fileReader.readAsDataURL(file)
					}, function(error) {
						 reject(error)
					})
				}, function(error) {
					reject(error)
				})
			},function(error) {
				console.log(error)		
			});
			return
        }
        if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
            wx.getFileSystemManager().readFile({
                filePath: path,
                encoding: 'base64',
                success: function(res) {
                    resolve('data:image/png;base64,' + res.data)
                },
                fail: function(error) {
                    reject(error)
                }
            })
            return
        }
        reject(new Error('not support'))
    })
}